Math.(floor/ceil/round) in JavaScript


Posted by hoyi-23 on 2021-07-11

Math.floor

執行向下捨入;會給出大小於或等於這個數值的整數。

console.log(Math.floor(0.5)); // 0
console.log(Math.floor(0.9)); // 0
console.log(Math.floor(1)); // 1

Math.ceil

執行向上捨入;會給出大於或等於這個數值的整數。

console.log(Math.ceil(0.5)); // 1
console.log(Math.ceil(0.9)); // 1
console.log(Math.ceil(1)); // 1

Math.round

執行標準四捨五入;會給出最接近的整数。

console.log(Math.round(0.4)); // 0
console.log(Math.round(0.5)); // 1
console.log(Math.round(0.9)); // 1
console.log(Math.round(1)); // 1

#Math.floor #Math.ceil #Math.round







Related Posts

軟體工程師面試資源最簡整理與技巧分享

軟體工程師面試資源最簡整理與技巧分享

原子習慣:基本原理 - 為何細微改變會帶來巨大差異?

原子習慣:基本原理 - 為何細微改變會帶來巨大差異?

[筆記]TCL 02 - Regexp(RE)的用法

[筆記]TCL 02 - Regexp(RE)的用法


Comments